using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.VisualBasic;
using Microsoft.Reporting.WebForms;
using System.Text;

/// <summary>
/// Summary description for ReportPrintHelper
/// </summary>
/// 
public enum EXPORT_TYPE
{
    TYPE_PDF = 1,
    TYPE_XLS = 2
}

public class ReportPrintHelper
{
	public ReportPrintHelper()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public void ExportReport(ref ReportViewer oViewer, EXPORT_TYPE ExportType, HttpContext oContext, bool bDownloadAttachment, string ReportWidth)
    {
        try
        {
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string filenameExtension;
            Byte[] pdfContent = null;
            StringBuilder deviceInfosb = new StringBuilder();
            deviceInfosb.Append("<DeviceInfo>");
            deviceInfosb.Append("<OutputFormat>EMF</OutputFormat>");
            deviceInfosb.Append(string.Format("<PageWidth>{0}in</PageWidth>", ReportWidth)); //PageWidth));
            deviceInfosb.Append(string.Format("<PageHeight>{0}in</PageHeight>", 8.27f));// PageHeight));
            deviceInfosb.Append(string.Format("<MarginTop>{0}in</MarginTop>", 0.5f));//MarginTop));
            deviceInfosb.Append(string.Format("<MarginLeft>{0}in</MarginLeft>", 0.5f));// MarginLeft));
            deviceInfosb.Append(string.Format("<MarginRight>{0}in</MarginRight>", 0.5f));// MarginRight));
            deviceInfosb.Append(string.Format("<MarginBottom>{0}in</MarginBottom>", 0.5f));// MarginBottom));
            deviceInfosb.Append(string.Format("</DeviceInfo>"));
            string deviceInfo = deviceInfosb.ToString();
            if (ExportType == EXPORT_TYPE.TYPE_PDF)
            {
                pdfContent = oViewer.LocalReport.Render("PDF", deviceInfo, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
                oContext.Response.Clear();
                oContext.Response.ContentType = "application/pdf";

                if (bDownloadAttachment)
                    oContext.Response.AddHeader("Content-disposition", "attachement;filename=report.pdf");

                oContext.Response.BinaryWrite(pdfContent);
                oContext.Response.End();
            }
            else if (ExportType == EXPORT_TYPE.TYPE_XLS)
            {
                pdfContent = oViewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
                oContext.Response.Clear();
                oContext.Response.ContentType = "application/Excel";

                if (bDownloadAttachment)
                    oContext.Response.AddHeader("Content-disposition", "attachement;filename=report.xls");

                oContext.Response.BinaryWrite(pdfContent);
                oContext.Response.End();
            }
        }
        catch (Exception Ex)
        {
            LogWriter.WriteLog("ExportReport : Error : " + Ex.Message);
        }
    }

}
